$(document).ready(function() {
	$("#adminCode").toggle();
	$(".register").toggle();
	$("#show-login").toggle();
	$("#show-login-panel").toggle();
	
});

function switchBoxes() {
	$("#show-register").toggle();
	$("#show-login").toggle();
	$("#show-register-panel").toggle();
	$("#show-login-panel").toggle();
	$(".register").toggle();
	$(".login").toggle();
}

function register() {
	data = $(".register-form").serialize();
	$.ajax({
			type:"POST",
			url:$("#project-base").val()+'Users',
			async: false,
			data: data,
			success:function(msg) {
				window.location.href=$("#project-base").val();
			},
			error:function(msg) {
				$("#register-error").html(msg.responseText);
			}
		});
}

function showAdminCode() {
	$("#showAdminCode").toggle();
	$("#adminCode").toggle();
}

function showSecretCode() {
	$("#secret-code").toggle();
}

function handleDB() {
	// if pressed enter
	if ($("#secret-code").val()=="1234") {
		restartDB();
	} else if ($("#secret-code").val()=="0000") {
		defaultDB();
	}else {
		$("body").addClass("darken");
		alert("Bad password...");
		$("body").removeClass("darken");
		return;
	}
	
}
	
function restartDB() {
	$("body").addClass("darken");
	if (!confirm("Are you sure you want to restart the db? (this may take a while")) {
		$("body").removeClass("darken");
		return;
	}
	$.ajax({
		type:"POST",
		url:$("#project-base").val()+'Users',
		async: true,
		data: "action="+$("#restart-action").val(),
		success:function(msg) {
			$("body").removeClass("darken");
			window.location.reload();
		},
		error:function(msg) {
			alert("Failed to restart db");
			$("body").removeClass("darken");
		}
	});
};

function defaultDB() {
	$("body").addClass("darken");
	if (!confirm("Are you sure you want to default the db? (this may take a while")) {
		$("body").removeClass("darken");
		return;
	}
	$.ajax({
		type:"POST",
		url:$("#project-base").val()+'Users',
		async: true,
		data: "action="+$("#default-action").val(),
		success:function(msg) {
			$("body").removeClass("darken");
			window.location.reload();
		},
		error:function(msg) {
			alert("Failed to default db");
			$("body").removeClass("darken");
		}
	});
};